<?php

/**
 * 商品控制器
 * @author yff
 * @date 2018-9-20 11:16:53
 */

namespace App\Http\ManagerApi\Controllers;

use App\Api\BaseClass\Controller;
use App\Http\ManagerApi\Models\PlatformGoodsCategory;
use Illuminate\Support\Facades\DB;
use App\Http\ManagerApi\Models\PlatformGoods;
use App\Api\Contracts\FileContract;

class GoodsController extends Controller
{
    /**
     * 分类列表
     */
    public function listCategory()
    {
        return $this->form->getMessage(PlatformGoodsCategory::where('parent_id', 0)->get());
    }

    /**
     * 添加分类
     */
    public function addCategory()
    {
        $rules = [['category_name', 'required']];

        $params = $this->form->camelFormOrFail($rules);

        if (PlatformGoodsCategory::where('category_name', $params['category_name'])->exists()) {
            return $this->form->error('分类名已存在');
        }

        return $this->form->createMessage(PlatformGoodsCategory::create($params));
    }

    /**
     * 分类详情
     */
    public function categoryDetail()
    {
        $rules = [['id', 'required|integer']];

        $params = $this->form->camelFormOrFail($rules);

        return $this->form->getMessage(PlatformGoodsCategory::find($params['id']));
    }

    /**
     * 编辑分类
     */
    public function updateCategory()
    {
        $rules = [
            ['id', 'required|integer'],
            ['category_name', 'string'],
        ];

        $params = $this->form->camelFormOrFail($rules);

        $category = PlatformGoodsCategory::findOrFail($params['id']);
        if (isset($params['category_name']) && $params['category_name'] != $category->category_name && PlatformGoodsCategory::where('category_name', $params['category_name'])->exists()) {
            return $this->form->error('该分类名已存在');
        }

        return $this->form->updateMessage($category->fill($params)->save());
    }

    /**
     * 删除分类
     */
    public function deleteCategory()
    {
        $rules = [['id', 'required|integer']];

        $params = $this->form->camelFormOrFail($rules);

        DB::transaction(function () use ($params) {
            PlatformGoodsCategory::where('id', $params['id'])->orWhere('parent_id', $params['id'])->delete();
            PlatformGoods::where('primary_category_id', $params['id'])->orWhere('second_category_id', $params['id'])->delete();
        });

        return $this->form->success('删除成功');
    }

    /**
     * 商品列表
     */
    public function listGoods()
    {
        $rules = [['primary_category_id', 'required|integer']];

        $params = $this->form->camelFormOrFail($rules);

        return $this->form->getMessage(PlatformGoods::where('primary_category_id', $params['primary_category_id'])->get());
    }

    /**
     * 添加商品
     */
    public function addGoods()
    {
        $rules = [
            ['primary_category_id', 'required|integer'],
            ['goods_name', 'required'],
            ['goods_price', 'required'],
            ['goods_introduction', 'required'],
            ['goods_image', 'required'],
        ];

        $params = $this->form->camelFormOrFail($rules);

        $desc = $params['goods_introduction'];
        unset($params['goods_introduction']);
        $goods = PlatformGoods::create($params);
        if ($goods) {

            $path = 'editor/' . $goods->id . '.html';
            file_put_contents($path, $desc);
            $goods->goods_introduction = $path;
            $goods->save();
        }

        return $this->form->createMessage($goods);
    }

    /**
     * 商品详情
     */
    public function goodsDetail()
    {
        $rules = [['id', 'required|integer']];

        $params = $this->form->camelFormOrFail($rules);

        return $this->form->getMessage(PlatformGoods::find($params['id']));
    }

    /**
     * 编辑商品
     */
    public function updateGoods()
    {
        $rules = [
            ['id', 'required|integer'],
            ['primary_category_id', 'integer'],
            ['goods_name', 'string'],
            ['goods_price', 'string'],
            ['goods_introduction', 'string'],
            ['goods_image', 'string'],
        ];

        $params = $this->form->camelFormOrFail($rules);

        if (count($params) < 2) {
            return $this->form->error('请至少修改一项!');
        }

        $desc = $params['goods_introduction'];
        unset($params['goods_introduction']);

        $goods = PlatformGoods::findOrFail($params['id']);
        file_put_contents($goods->goods_introduction, $desc);

        return $this->form->updateMessage($goods->fill($params)->save());
    }

    /**
     * 删除商品
     */
    public function deleteGoods()
    {
        $rules = [['id', 'required|integer']];

        $params = $this->form->camelFormOrFail($rules);

        return $this->form->deleteMessage(PlatformGoods::destroy($params['id']));
    }

    /**
     * 图片上传
     */
    public function imageUpload(FileContract $file)
    {
        $rules = [['file', 'required']];

        $params = $this->form->camelFormOrFail($rules);

        $path = $file->saveImageTo('file', 'upload');

        return $file->isError($path) ? $this->form->error('上传失败') : $this->form->datas($path);
    }
}